<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

$menu_data = menu_get_item($current_page, "en");
$menu_entry = $menu_data[0];
$menu_parent = $menu_data[1];
$menu_rank = $menu_data[2];
$menu_hidden = $menu_data[4];

$r = $db->get_values_c( "pages", "id = $current_page AND (lang = \"en\" OR lang = \"en_US\")" );

if (!$r[id]) {
    $db->exec( "INSERT INTO pages (id, title, content, lang) VALUES ($current_page, \"\", \"\", \"en\") ");
    $r = $db->get_values_c( "pages", "id = $current_page AND lang = \"en\"" );
}

$site_title = _("Edit")." ("._("English").") - ".$r[title];

include("admin/edit-tinymce.php");

//$site_head_extra = "<script type=\"text/javascript\" src=\"ext_lib/ckeditor/ckeditor_basic.js\"></script>

$options = "<option value=-1 "; if ( $menu_parent == -1 ) $options .=" selected"; $options .= ">"._("No parent")."</option>\n";

function print_children($id){
    global $menu_levels, $site_menus, $options, $menu_parent;
    $n = $menu_levels[$id];
    $tab = "";
    for ($c = 1; $c <= $n; $c++){
        $tab .= "--";
    }
    $options .= "<option value=$id";
    if ( $menu_parent == $id ) $options .=" selected";
    $options .= ">".trim($tab." ".$site_menus[$id][0])." (p$id)"."</option>";

    if (count(menu_get_children($id)) > 0){
        foreach(menu_get_children($id) as $id){
            print_children($id);
        }
    }
}
foreach($site_menus as $id => $a){
    //echo $a[0]." ".count(menu_get_children($id))." ".menu_get_parent($id)." \n";
    if ($menu_levels[$id] == 0 ){
        //echo $site_menus[$id][0]."\n";
        print_children($id);
    }
}
foreach($site_menus as $id => $a){
    if ( $id != $current_page ){
        //$options .= "<option value=$id";if ( $menu_parent == $id ) $options .=" selected"; $options .= ">Page $id: ${a[0]}</option>\n";
    }
}

if ($menu_hidden == "1"){
    $hidden_checked = "checked";
} else {
    $hidden_checked = "";
}

$site_content = $editor.'
        <form method="post">
            <table class="admin-edit-page">
                <tr>
                    <td width="30%"> '._("Title").': </td>
                    <td width="40%"> <input name="edit-title" type="text" size="40%" value="'.html_entity_decode($r[title]).'"/> </td>
                    <td width="30%"></td>
                </tr>
                <tr>
                    <td>'._("Menu entry").':</td>
                    <td><input name="edit-menu-title" type="text" size="40%" value="'.$menu_entry.'"/> </td>
                    <td><div class="admin-comment">('._("Leave empty for none").')</div></td>
                </tr>
                <tr>
                    <td>'._("Menu rank").':</td>
                    <td><input name="edit-menu-rank" type="text" size="5" value="'.$menu_rank.'"/></td>
                </tr>
                <tr>
                    <td>'._("Parent item").':</td>
                    <td><select name="edit-menu-parent">'.$options.'
                    </select></td>
                </tr>
                <tr>
                    <td>'._("Set as default?").'</td>
                    <td><input name="edit-default" type="checkbox"/></td>
                    <td></td>
                </tr>
                <tr>
                    <td>'._("Hidden?").'</td>
                    <td><input name="edit-hidden" type="checkbox" '.$hidden_checked.' /></td>
                    <td>('._("Menu items are only shown when in admin mode").')</td>
                </tr>
            </table>
            <p></p>
            <p></p>
            <p></p>
            <p>
                '._("Site content").':<br />
                <textarea style="width:100%;height=300px;" name="edit-content">'.$r[content].'</textarea>';

/*
$site_content .= '<script type="text/javascript">
    CKEDITOR.replace( "edit-content" );
</script>';
*/

$site_content .= '
            </p>
            <p>
                <input type="hidden" name="edit-id" value="'.$current_page.'"/>
                <input type="submit" name="submit-edit" value="'."Submit".'"/>
            </p>
        </form>';
?>
